<?php
declare(strict_types=1);

namespace app\validate;

use think\Validate;
use app\service\OrderService;

class OrderValidate extends Validate
{
    protected $rule = [
        'merchant_id' => 'require|number',
        'company_code' => 'require',
        'business_type' => 'require',
        'sender_name' => 'require|max:50',
        'sender_phone' => 'require|mobile',
        'sender_province' => 'require',
        'sender_city' => 'require',
        'sender_county' => 'require',
        'sender_address' => 'require|max:200',
        'receiver_name' => 'require|max:50',
        'receiver_phone' => 'require|mobile',
        'receiver_province' => 'require',
        'receiver_city' => 'require',
        'receiver_county' => 'require',
        'receiver_address' => 'require|max:200',
        'weight_estimate' => 'require|float|gt:0',
        'insure_amount' => 'float|egt:0',
        'payment_method' => 'require|in:1,2,3',
        'pickup_date' => 'require|checkPickupDate',
        'pickup_start_time' => 'require|checkPickupTime',
        'cargo_type' => 'require|in:1,2,3,4,5,6,7,99',
        'cargo_name' => 'require|max:50',
        'cargo_custom_type' => 'requireIf:cargo_type,99|max:50',
        'cargo_quantity' => 'require|number|gt:0',
        'cargo_remark' => 'max:200'
    ];

    protected $message = [
        'merchant_id.require' => '商家ID不能为空',
        'company_code.require' => '快递公司不能为空',
        'business_type.require' => '业务类型不能为空',
        'sender_name.require' => '寄件人姓名不能为空',
        'sender_name.max' => '寄件人姓名最多50个字符',
        'sender_phone.require' => '寄件人电话不能为空',
        'sender_phone.mobile' => '寄件人电话格式不正确',
        'sender_province.require' => '寄件人省份不能为空',
        'sender_city.require' => '寄件人城市不能为空',
        'sender_county.require' => '寄件人区县不能为空',
        'sender_address.require' => '寄件人详细地址不能为空',
        'sender_address.max' => '寄件人详细地址最多200个字符',
        'receiver_name.require' => '收件人姓名不能为空',
        'receiver_name.max' => '收件人姓名最多50个字符',
        'receiver_phone.require' => '收件人电话不能为空',
        'receiver_phone.mobile' => '收件人电话格式不正确',
        'receiver_province.require' => '收件人省份不能为空',
        'receiver_city.require' => '收件人城市不能为空',
        'receiver_county.require' => '收件人区县不能为空',
        'receiver_address.require' => '收件人详细地址不能为空',
        'receiver_address.max' => '收件人详细地址最多200个字符',
        'weight_estimate.require' => '预估重量不能为空',
        'weight_estimate.float' => '预估重量必须是数字',
        'weight_estimate.gt' => '预估重量必须大于0',
        'insure_amount.float' => '保价金额必须是数字',
        'insure_amount.egt' => '保价金额必须大于等于0',
        'payment_method.require' => '支付方式不能为空',
        'payment_method.in' => '不支持的支付方式',
        'pickup_date.require' => '预约日期不能为空',
        'pickup_start_time.require' => '预约时间不能为空',
        'cargo_type.require' => '请选择货物类型',
        'cargo_type.in' => '货物类型不正确',
        'cargo_custom_type.requireIf' => '请填写具体物品类型',
        'cargo_custom_type.max' => '物品类型最多50个字符',
        'cargo_name.require' => '请填写货物名称',
        'cargo_name.max' => '货物名称最多50个字符',
        'cargo_quantity.require' => '请填写货物数量',
        'cargo_quantity.number' => '货物数量必须为数字',
        'cargo_quantity.gt' => '货物数量必须大于0',
        'cargo_remark.max' => '备注最多200个字符'
    ];

    // 验证预约日期
    protected function checkPickupDate($value): bool
    {
        try {
            $orderService = new OrderService();
            return $orderService->validatePickupTime($value, $this->data['pickup_start_time']);
        } catch (\Exception $e) {
            $this->message['pickup_date'] = $e->getMessage();
            return false;
        }
    }

    // 验证预约时间
    protected function checkPickupTime($value): bool
    {
        // 验证时间格式
        if (!preg_match('/^([01][0-9]|2[0-3]):[0-5][0-9]$/', $value)) {
            $this->message['pickup_start_time'] = '预约时间格式不正确';
            return false;
        }
        return true;
    }

    // 创建订单场景
    public function sceneCreate()
    {
        return $this->only([
            'merchant_id','company_code','business_type',
            'sender_name','sender_phone','sender_province','sender_city','sender_county','sender_address',
            'receiver_name','receiver_phone','receiver_province','receiver_city','receiver_county','receiver_address',
            'weight_estimate','insure_amount','payment_method','pickup_date','pickup_start_time'
        ]);
    }

    // 取消订单场景
    public function sceneCancel()
    {
        return $this->only(['order_no']);
    }
} 